﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Gridify.Controllers;

namespace Gridify
{
    public static class GridExtensions
    {
        private const string GridViewDataId = "__GridifierId";

        public static IEnumerable<T> Gridify<T>(this Controller instance, IEnumerable<T> items, int pageSize, string viewTemplateName)
        {
            var gridData = new GridData<T>(items.Take(1024).ToList())//We don't want to accidently store millions of records here. 
                               {
                                   Id = Guid.NewGuid(),
                                   PageSize = pageSize,
                                   ViewTemplateName = viewTemplateName
                               };

            instance.HttpContext.Cache[gridData.Id.ToString("N")] = gridData;
            instance.ViewData[GridViewDataId] = gridData;

            return gridData.Items.Take(pageSize);
        }

        public static MvcHtmlString GridifierStyles<T>(this HtmlHelper<T> instance)
        {
            var gridData = instance.ViewData[GridViewDataId] as IGridData;

            if (gridData != null)
            {
                var urlHelper = new UrlHelper(instance.ViewContext.RequestContext);
                var stylesLocation = urlHelper.Action("Styles", "Gridify");
                var styleLink = "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + stylesLocation + "\">";

                return MvcHtmlString.Create(styleLink);
            }

            return MvcHtmlString.Empty;
        }

        public static MvcHtmlString GridifierScripts<T>(this HtmlHelper<T> instance, string gridClass = "gridify")
        {
            var gridData = instance.ViewData[GridViewDataId] as IGridData;
            
            if (gridData != null)
            {
                var urlHelper = new UrlHelper(instance.ViewContext.RequestContext);
                var pageLink = urlHelper.Action("Page", "Gridify");
                var scriptLocation = urlHelper.Action("Scripts", "Gridify");
                var scriptIncluder = "<script type=\"text/javascript\" src=\"" + scriptLocation + "\"></script>";
                var script = "<script type=\"text/javascript\">\n" +
                             " $(function() {\n" +
                             "    $('." + gridClass + "').gridify({ " +
                             "          gridId: '" + gridData.Id.ToString("N") + "', " +
                             "          pageLink: '" + pageLink + "', " +
                             "          totalPages: '" + gridData.TotalPages + "', " +
                             "          pageSize: '" + gridData.PageSize + "'" +
                             "    })\n" +
                             " });\n" +
                             "</script>";
                return MvcHtmlString.Create(scriptIncluder + script);
            }

            return MvcHtmlString.Empty;
        }
    }
}